/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
package awd.bj.jls.webs.controller;

import awd.bj.jls.model.JbxxModel;
import awd.bj.jls.model.WpglModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.*;
import awd.framework.common.utils.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jbxxRsdj")
@RefreshScope
public class JbxxRsdjAction extends BaseAction {

    @Autowired
    private JlsServersApi jlsServersApi;

    @RequestMapping({"/jbxxRsdj.html" //首页
            ,"/jbxxRsdj_enter.html" //单表录入页面
            ,"/jbxxRsdj_jbxx_enter.html" //人员信息查询页面
            ,"/jbxxRsdj_query.html" //信息查询
            ,"/jbxxRsdj_jbxx_query.html" //关联基本信息查询
            ,"/6.html" //
            ,"/7.html"})
    public String index(Model model, HttpServletRequest request) {
        model.addAllAttributes(getBaseModelData());
        return getRenderFilePath(request);
    }

    @PostMapping("/saveForRsdj")
    @ResponseBody
    public Result<?> saveForRsdj(HttpServletRequest request,String jbxxDate,String ajxxDate,MultipartHttpServletRequest multiRequest) {
    	  JbxxModel jbxx= JSON.parseObject(jbxxDate,JbxxModel.class);
    	  JbxxModel ajxxs= JSON.parseObject(ajxxDate,JbxxModel.class);
    	  String photosids = request.getParameter("photosid");
    	  String[] ids = photosids.split(",") ;
    	  int i = 0;
    	  BeanUtils.copyProperties(ajxxs, jbxx);
    	  System.err.println("jbxxModel---"+JSON.toJSONString(jbxx));
    	  System.err.println("jbxxModel---"+jbxx.getId());
    	  jlsServersApi.jbxxUpdate(jbxx.getId(), jbxx);
    	  //照片处理
    	  Iterator<String> iterable = multiRequest.getFileNames();
          System.err.println("iterable----"+JSON.toJSONString(iterable.hasNext()));
          while (iterable.hasNext()) {
        	  System.err.println("-----迭代中------");
        	//取出单个文件
              MultipartFile file = multiRequest.getFile(iterable.next());
        	  jlsServersApi.uploadPhotoById(ids[i], file);
        	  i++;
          }
		return null;
    	
    	
    }
    
    
}